package controller

import (
	"blog2021/model"
	"blog2021/tools/logger"
	"blog2021/tools/timeutil"
	"fmt"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func BingToday(c *gin.Context) {
	wallpaper, err := model.GetBingWallpaper()
	if err != nil {
		logger.Error(err)
		c.JSON(400, err)
		return
	}
	c.PureJSON(200, wallpaper)
}

func BingDown(c *gin.Context) {
	err := model.DownloadBingWallpaper()
	if err != nil {
		ResponseMessage(c, "ERROR", err.Error())
		return
	}

	c.JSON(200, "Ok")
}

func BingPage(c *gin.Context) {
	page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
	if err != nil {
		page = 1
	}

	bl, err := model.GetBingPage(page)
	if err != nil {
		ResponseMessage(c, "ERROR", err.Error())
		return
	}

	content := gin.H{
		"PrevPage": page - 1,
		"NextPage": page + 1,
		"List":     bl,
	}

	c.HTML(http.StatusOK, "gallery.html", gin.H{
		"content":          content,
		"HideRightSidebar": true,
	})
}

func BingDate(c *gin.Context) {
	date := c.DefaultQuery("date", timeutil.FormatDate(time.Now()))
	bing, err := model.GetBingDate(date)
	if err != nil {
		c.JSON(400, fmt.Sprintf("获取壁纸失败，日期：%s", date))
		return
	}
	c.JSON(200, bing)
}
